using System;
using System.IO;
using System.Text;

namespace AutoUpdater
{
    /// <summary>
    /// 日志工具类
    /// </summary>
    public static class Logger
    {
        private static string _logDirectory;
        private static string _logFilePath;
        private static object _lockObj = new object();
        private static bool _isInitialized = false;

        /// <summary>
        /// 初始化日志系统
        /// </summary>
        public static void Initialize()
        {
            if (_isInitialized) return;

            try
            {
                // 创建日志目录
                _logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
                if (!Directory.Exists(_logDirectory))
                {
                    Directory.CreateDirectory(_logDirectory);
                }

                // 日志文件命名（按日期）
                string fileName = $"AutoUpdate_{DateTime.Now:yyyyMMdd}.log";
                _logFilePath = Path.Combine(_logDirectory, fileName);

                _isInitialized = true;
                Info("日志系统初始化完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"日志系统初始化失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 记录普通信息
        /// </summary>
        public static void Info(string message)
        {
            WriteLog("INFO", message);
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        public static void Warn(string message)
        {
            WriteLog("WARN", message);
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        public static void Error(string message)
        {
            WriteLog("ERROR", message);
        }

        /// <summary>
        /// 记录错误信息（带异常）
        /// </summary>
        public static void Error(Exception ex)
        {
            WriteLog("ERROR", $"{ex.Message}\n{ex.StackTrace}");
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        public static void Fatal(Exception ex, string message)
        {
            WriteLog("FATAL", $"{message}\n{ex.Message}\n{ex.StackTrace}");
        }

        /// <summary>
        /// 记录状态变更
        /// </summary>
        public static void LogStatus(UpgradeStatus status)
        {
            WriteLog("STATUS", $"升级状态变更为：{status}");
        }

        /// <summary>
        /// 写入日志到文件
        /// </summary>
        private static void WriteLog(string level, string message)
        {
            if (!_isInitialized)
            {
                Initialize();
                if (!_isInitialized)
                {
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}[{level}] {message}");
                    return;
                }
            }

            try
            {
                lock (_lockObj)
                {
                    string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}][{level}] {message}\r\n";
                    
                    // 写入文件
                    File.AppendAllText(_logFilePath, logEntry, Encoding.UTF8);
                    
                    // 同时输出到控制台
                    Console.Write(logEntry);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"写入日志失败: {ex.Message}");
                Console.WriteLine($"日志内容: [{level}] {message}");
            }
        }

        /// <summary>
        /// 关闭日志系统
        /// </summary>
        public static void Shutdown()
        {
            if (_isInitialized)
            {
                Info("日志系统已关闭");
                _isInitialized = false;
            }
        }
    }
}
    